
Imports cmplib01
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary


<Serializable()> Public Class CampaignerTemplate

    Private _units As New UnitTypes_col
    Private _install As New ObjectiveTypes_col
    Private _rules As New RuleTypes
    Private _resources As New ResourceTypes
    Private _elements As New ElementManager

    Public Property elements() As ElementManager
        Get
            Return _elements
        End Get
        Set(ByVal value As ElementManager)
            _elements = value
        End Set
    End Property

    Public Property Units() As UnitTypes_col
        Get
            Return _units
        End Get
        Set(ByVal value As UnitTypes_col)
            _units = value
        End Set
    End Property

    Public Property installations() As ObjectiveTypes_col
        Get
            Return _install
        End Get
        Set(ByVal value As ObjectiveTypes_col)
            _install = value
        End Set
    End Property

    Public Property rules() As RuleTypes
        Get
            Return _rules
        End Get
        Set(ByVal value As RuleTypes)
            _rules = value
        End Set
    End Property

    Public Property Resources() As ResourceTypes
        Get
            Return _resources
        End Get
        Set(ByVal value As ResourceTypes)
            _resources = value
        End Set
    End Property


    Public Sub Save(ByVal path As String)
        Try
            Try
                Dim fs As New FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)
                Dim bin As New BinaryFormatter

                bin.Serialize(fs, Me)
            Catch ex As System.IO.IOException
                Kill(path)
                Dim fs As New FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)
                Dim bin As New BinaryFormatter

                bin.Serialize(fs, Me)
            Catch ex As Exception
                Throw ex
            End Try

        Catch ex As Exception
            HandleException("CampaignerTemplate.Save", ex)
        End Try
    End Sub

    Public Function Load(ByVal path As String) As CampaignerTemplate
        Dim fs As New FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim bin As New BinaryFormatter
        Dim template As New CampaignerTemplate

        Try
            template = CType(bin.Deserialize(fs), CampaignerTemplate)
            Return template
        Catch ex As Exception
            HandleException("CampaignerTemplate.load", ex)
        End Try
    End Function

End Class
